package com.gowants.pems.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gowants.pems.mapper.ErrorCodeMapper;
import com.gowants.pems.model.ErrorCode;
import com.gowants.pems.util.RedisUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * <提示信息服务>
 *
 * @ClassName ErrorCodeService
 * @remake create by GJR 2019/6/3 11:17
 */
@Service(value = "errorCodeService")
public class ErrorCodeService extends NewBaseService<ErrorCode> {

	@Autowired
	private ErrorCodeMapper errorCodeMapper;
	@Autowired
	private RedisUtils redisUtils;
	/**
	 * key 前缀，防止和其他地方的key可以冲突
	 */
	@Value("${gowants.redisKey}")
	private String redisKey;
	@Override
	public int save(ErrorCode record) {
		int i = 0;
		try {
			i = errorCodeMapper.save(record);
			// 先查询redis
			Object codeJsons = redisUtils.get(redisKey+"errorCode:" + record.getErrorCode());
			if (null != codeJsons) {
				redisUtils.del(redisKey+"errorCode:"+record.getErrorCode());
			}
			Random r = new Random();
			redisUtils.set(redisKey+"errorCode:" + record.getErrorCode(), JSON.toJSON(record), r.nextInt(999999));
			return i;

		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public int delete(String primaryKey) {
		int i = 0;
		try {
			ErrorCode ec=errorCodeMapper.getId(Integer.valueOf(primaryKey));
			i = errorCodeMapper.delete(Integer.valueOf(primaryKey));
			// 先查询redis
			Object codeJsons = redisUtils.get(redisKey+"errorCode:" + ec.getErrorCode());
			if (null != codeJsons) {
				redisUtils.del(redisKey+"errorCode:"+ec.getErrorCode());
			}
			return i;

		} catch (Exception e) {
			return 0;
		}
	}

	@Override
	public int update(ErrorCode record) {
		int i=0;
		i= errorCodeMapper.update(record);
		// 先查询redis
		Object codeJsons = redisUtils.get(redisKey+"errorCode:" + record.getErrorCode());
		if (null != codeJsons) {
			redisUtils.del(redisKey+"errorCode:"+record.getErrorCode());
		}
		Random r = new Random();
		redisUtils.set(redisKey+"errorCode:" + record.getErrorCode(), JSON.toJSON(record), r.nextInt(999999));
		return i;
	}

	@Override
	public ErrorCode selectOne(String errorCode) {
		String codeJson = "";
		Object codeJsons = redisUtils.get(redisKey+"errorCode:" + errorCode);
		if (null != codeJsons) {
			codeJson = codeJsons.toString();
		}
		if (StringUtils.isNotBlank(codeJson)) {
			ErrorCode ec = JSONObject.parseObject(codeJson, ErrorCode.class);
			return ec;
		} else { 
			ErrorCode ec=errorCodeMapper.getErrorCode(errorCode);
			if (null != ec) {
				Random r = new Random();
				redisUtils.set(redisKey+"errorCode:" + errorCode, JSON.toJSON(ec), r.nextInt(999999));
				return ec;
			}
		}
		return null;
	}

	@Override
	public List<ErrorCode> select(ErrorCode record) {
		// return null;
		return errorCodeMapper.getList(record);
	}

	@Override
	public int getCount(ErrorCode record) {
		return 0;
	}

	/**
	 * <获取渠道对应得提示信息>
	 *
	 * @param
	 * @return
	 * @remake create by GJR 2019/6/3 11:29
	 */
	public ErrorCode getErrorCodeByChannel(String errorCode, String channel) {
		try {
			// 先查询redis
			String codeJson = "";
			Object codeJsons = redisUtils.get(redisKey+"errorCode:" + errorCode);
			if (null != codeJsons) {
				codeJson = codeJsons.toString();
			}
			if (StringUtils.isNotBlank(codeJson)) {
				ErrorCode ec = JSONObject.parseObject(codeJson, ErrorCode.class);
				return ec;
			} else {
				ErrorCode ec = errorCodeMapper.getErrorCodeByChannel(errorCode, channel);
				if (null != ec) {
					Random r = new Random();
					redisUtils.set(redisKey+"errorCode:" + errorCode, JSON.toJSON(ec), r.nextInt(999999));
					return ec;
				}
			}
		} catch (Exception e) {
			return null;
		}
		return null;

	}

	/// 按照错误码降序后取第一条数据
	public ErrorCode getTopOneByCode() {
		return errorCodeMapper.getTopOneByCode();
	}
}
